﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using homework05;
using static homework05.Order;

//f1.为订单管理的程序添加一个WinForm的界面。通过这个界面，调用OrderService的各个方法，实现创建订单、删除订单、修改订单、查询订单、导出订单、导入订单等功能。
//要求：
//（1）WinForm的界面部分单独写一个项目，依赖于原来的项目。
//（2）可以使用两个窗口：主窗口实现查询展示功能，以及放置各种功能按钮；新建 / 修改订单功能使用弹出窗口。
//（3）注意窗口的布局，在窗口尺寸变化时，不出现错位挤压等情况。
//（4）尽量通过数据绑定来实现功能。订单和订单明细各使用一个bindingsource，通过DataMember实现主从对象绑定。

//f2.编写程序，界面包含一个标签、一个文本框和一个按钮，当用户单击按钮时，程序把文本框中的内容复制到标签中。





namespace orderUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public OrderService service;
        public OrderData orderData;
        public Order now;
        public MainWindow()
        {
            InitializeComponent();
            service = new OrderService();
            service.Import();
            orderData = new OrderData()
            {
                GoodsName = "",
                Number = "",
                User = "",
                Query = "",
                Log = "显示所有信息可在查询中使用 *, 双击切换订单",
                OrderList = service.OrderList,
                CargoList = service.OrderList[0].Items,
            };

            goods.DataContext = orderData;
            number.DataContext = orderData;
            user.DataContext = orderData;
            query.DataContext = orderData;
            log.DataContext = orderData;
            orderList.DataContext = orderData;
            cargoList.DataContext = orderData;
            now = orderData.OrderList[0];
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            service.AddOrder(new Order(orderData.User));
            orderList.ItemsSource = null;
            orderList.ItemsSource = service.OrderList;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            try {
                service.DelOrder(now.ID);
            }catch(OrderInvalidException e3)
            {
                log.Content = e3.Message;
            }
            orderList.ItemsSource = null;
            orderList.ItemsSource = service.OrderList;
        }

        private void orderList_MouseDown(object sender, MouseButtonEventArgs e)
        {
            DataGridRow row = ItemsControl.ContainerFromElement((DataGrid)sender, e.OriginalSource as DependencyObject) as DataGridRow;
            if (row == null) return;
            int index = row.GetIndex();
            string id = (orderList.Columns[1].GetCellContent(orderList.Items[index]) as TextBlock).Text;
            log.Content = id;
            now = service.getOrder(id);
            cargoList.ItemsSource = null;
            cargoList.ItemsSource = now.Items;
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            if(now == null)
            {
                log.Content = "未选中订单";
                return;
            }
            try {
                int temp = int.Parse(number.Text);
                now.AddItem(goods.Text, temp);
                cargoList.ItemsSource = null;
                cargoList.ItemsSource = now.Items;
            }
            catch (ItemExistsException ee)
            {
                log.Content = "商品已存在";
            }
            catch (CargoNotExistsException)
            {
                log.Content = "商品不存在";
            }
            catch (Exception ee)
            {
                log.Content = "error";
            }
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            try
            {
                int temp = int.Parse(number.Text);
                service.ModifyOrder(now.ID, goods.Text, temp);
                cargoList.ItemsSource = null;
                cargoList.ItemsSource = now.Items;
            }
            catch (OrderInvalidException ee)
            {
                log.Content = "不存在此商品";
            }
            catch (Exception ee)
            {
                log.Content = "error";
            }
            

        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            try
            {
                now.delItem(goods.Text);
                cargoList.ItemsSource = null;
                cargoList.ItemsSource = now.Items;
            }
            catch (ItemInvalidException ee)
            {
                log.Content = "不存在此商品";
            }
        }

        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            if (query.Text == "*")
            {
                orderList.ItemsSource = service.OrderList;
                return;
            }
            var list = (from o in service.OrderList
                       where o.ID == query.Text
                       select o).ToList();
            orderList.ItemsSource = list;
        }

        private void Button_Click_6(object sender, RoutedEventArgs e)
        {
            if (query.Text == "*")
            {
                cargoList.ItemsSource = now.Items;
                return;
            }
            var list = (from o in now.Items
                        where o.Pname == query.Text
                        select o).ToList();
            cargoList.ItemsSource = list;
        }

        private void Button_Click_7(object sender, RoutedEventArgs e)
        {
            if (query.Text == "*")
            {
                orderList.ItemsSource = service.OrderList;
                return;
            }
            var list = (from o in service.OrderList
                        where o.Customer == query.Text
                        select o).ToList();
            orderList.ItemsSource = list;
        }
    }
    public class OrderData
    {
        public string GoodsName { get; set; }
        public string Number { get; set; }
        public string User { get; set; }
        public string Query { get; set; }
        public string Log { get; set; }
        public List<Order> OrderList { get; set; }
        public List<OrderItem> CargoList { get; set; }
    }
}
